﻿using BBCore.Application.Admins;
using BBCore.Core.Models.Admins;
using Microsoft.AspNetCore.Components;
using System.Diagnostics.CodeAnalysis;

namespace BBCore.Server.Components.Shared
{
    public partial class AnnouncementDropdown
    {
        private List<Announcement>? Announcements { get; set; } = [];
        /// <summary>
        /// 未读消息条数
        /// </summary>
        private int UnreadCount { get; set; }
        /// <summary>
        /// 当前用户ID
        /// </summary>
        [Parameter]
        public int CurrentUserId { get; set; }
        [Inject]
        [NotNull]
        IAnnouncementsService? IAnnouncementsService { get; set; }
        protected override async Task OnInitializedAsync()
        {
            Announcements = await IAnnouncementsService.LoadAnnouncements_TopAsync();
            if (Announcements != null)
            {
                UnreadCount = Announcements.Count(a => !IsAnnouncementRead(a.Id));
            }

        }
        private bool IsAnnouncementRead(int announcementId)
        {
            return IAnnouncementsService.IsAnnouncementRead(announcementId, CurrentUserId);
        }
        private async Task MarkAsReadAsync(int announcementId)
        {
            if (!IsAnnouncementRead(announcementId))
            {
                await IAnnouncementsService.MarkAsReadAsync(announcementId, CurrentUserId);
                await IAnnouncementsService.LoadAnnouncements_TopAsync();
                UnreadCount--;


            }
        }

    }
}
